//
//  ICWebViewController+JSCallBack.m
//  ICome
//
//  Created by zhangrongwu on 2016/11/10.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICWebViewController+JSCallBack.h"
#import "ICActionSheetView.h"
#import "ICFileScanController.h"
#import "ICFileTool.h"
#import "iComeApiManager.h"

@implementation ICWebViewController (JSCallBack)

- (void)handleCall:(NSString *)functionName callbackId:(NSString *)callbackIds param:(NSDictionary *)param {
    self.callName = functionName;
    [self.callbackMap setObject:callbackIds forKey:functionName];
    if (![ICTools isObjEmpty:param]) {
        [self.jsRequestParam setObject:param forKey:functionName];
    }
    
    // 基础接口 开放式 1.webview.close 2.webview.getUserAgent 3.webview.back 4.device.getNetworkType
    if ([functionName isEqualToString:@"webview.close"]) { // 关闭webview
        [ICJSBridgeTools closeVC:self];
        NSDictionary *args = @{@"success":@true};
        [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.close"] args:args] toWebView:self.webView];
    }
    if ([functionName isEqualToString:@"webview.getUserAgent"]) {
        NSDictionary *args = @{@"success":@true,
                               @"error":@"",
                               @"data":@{@"userAgent":[ICJSBridgeTools getOS]}};
        [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.getUserAgent"] args:args] toWebView:self.webView];
    }
    
    else if ([functionName isEqualToString:@"webview.back"]) { // 返回上一页
        if ([self.webView canGoBack]) {
            [self.webView goBack];
            NSDictionary *args = @{@"success":@true};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.back"] args:args] toWebView:self.webView];
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"未能返回上一页"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.back"] args:args] toWebView:self.webView];
        }
    }
    
    else if ([functionName isEqualToString:@"device.getNetworkType"]) {// 获取网络类型
        NSDictionary *args = @{@"success":@true,
                               @"error":@"",
                               @"data":@{@"networkType":[ICJSBridgeTools getNetworkType]}};
        [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"device.getNetworkType"] args:args] toWebView:self.webView];
    }
    
    // 权限校验
    if ([[self.webView.URL absoluteString] hasPrefix:self.configURL]) { // 请求控制
        if ([functionName isEqualToString:@"validData"]) {// 权限控制validData
            
            [param setValue:self.permURL forKey:@"url"]; // 把对应的url放入 param
            [self.jsApiList addObjectsFromArray:[param objectForKey:@"jsApiList"]];
            NSDictionary *args = @{@"success":@true};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"validData"] args:args] toWebView:self.webView];
            
            //            [[ICApplicationNetworkManager sharedInstance] jsapiverifyWithObject:param success:^(id object) {
            //                NSLog(@"jsapiverify : %@", object);
            //                NSDictionary *data = [object objectForKey:@"data"];
            //                if (![ICTools isObjEmpty:data]) {
            //                    [self.jsApiList addObjectsFromArray:[data objectForKey:@"jsApiList"]];
            //                }
            //
            //                NSDictionary *args = @{@"success":@true};
            //                [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"validData"] args:args] toWebView:self.webView];
            //
            //            } failure:^(NSError *error) {
            //                NSDictionary *args = @{@"success":@false,
            //                                       @"data":@{@"errno":@"1",@"error":error.domain}};
            //                [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"validData"] args:args] toWebView:self.webView];
            //                [self jsBridge:nil didFailProvisionalNavigation:nil withError:nil];
            //                [MBProgressHUD showError:error.domain];
            //            }];
        }
    } else {
        if (![functionName isEqualToString:@"validData"]) {
            NSString *alertFuncName = [NSString stringWithFormat:@"%@无权限", functionName];
            NSLog(@"  %@ - %@ ", self.originalURL , alertFuncName);
        }
        return;
    }
    
    
    /******************************************权**限**控**制*******************************************/
    /******************************************权**限**控**制*******************************************/
    if(![self.jsApiList containsObject:functionName]) {
        return;
    }
    if ([functionName isEqualToString:@"webview.hideWebViewToolbar"]) {
        [self hideNavigationBar];
        NSDictionary *args = @{@"success":@true};
        [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.hideWebViewToolbar"] args:args] toWebView:self.webView];
    }
    
    else if ([functionName isEqualToString:@"webview.showWebViewToolbar"]) {
        [self showNavigationBar];
        NSDictionary *args = @{@"success":@true};
        [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.hideWebViewToolbar"] args:args] toWebView:self.webView];
    }
    
    else if ([functionName isEqualToString:@"device.selectPhotos"]) { // 拍照
        [[ICPhotoPickerManager shareInstace] showActionSheetInView:self.view maxCount:[param[@"amount"] integerValue] fromController:self completionBlock:^(NSMutableArray *imageArray) {
            @autoreleasepool {
                dispatch_group_t group = dispatch_group_create();
                
                NSMutableArray *baseList = [[NSMutableArray alloc] init];
                for (UIImage *image in imageArray) {
                    dispatch_group_enter(group);
                    UIImage *image_ = [UIImage fixOrientation:image];
                    // 压缩图片至指定大小
                    [image_ jkr_fastCompressToDataLength:200000 withBlock:^(NSData *data) {
                        NSString *imageDataBase64 = [NSString base64EncodedStringWithData:data];
                        NSString *imageDataStr = [NSString stringWithFormat:@"data:%@;base64,%@", [ICJSBridgeTools typeForImageData:data],imageDataBase64];
                        [baseList addObject:imageDataStr];
                        dispatch_group_leave(group);
                    }];
                }
                
                dispatch_group_notify(group, dispatch_get_main_queue(), ^{
                    NSDictionary *args = @{@"success":@true,
                                           @"error":@"",
                                           @"data":baseList};
                    [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"device.selectPhotos"] args:args] toWebView:self.webView];
                });
            }
        }];
    }
    
    else if ([functionName isEqualToString:@"system.getUserInfo"]) {// 获取个人信息
        if (![ICTools stringEmpty:[ICTools getToken]]) {
            NSDictionary *args = @{@"success":@true,
                                   @"error":@"",
                                   @"data":[ICJSBridgeTools getUserInfo]};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.getUserInfo"] args:args] toWebView:self.webView];
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"用户token无效"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.getUserInfo"] args:args] toWebView:self.webView];
        }
    }
    
    else if ([functionName isEqualToString:@"device.getLocation"]) { // 获取位置
        
    }
    
    else if ([functionName isEqualToString:@"setWebViewTitle"]) {
        self.title = param[@"title"];
    }
    
    else if ([functionName isEqualToString:@"system.selectUser"]) { // 获取 选择 用户
        ICOrganizationViewController *vc = [[ICOrganizationViewController alloc] init];
        vc.singleSelect = NO;
        vc.CtrStyle = CtrStyleSelectEmploye_sdk;
        vc.kkGroupType = GroupStyleSelectEmployee;
        vc.singleSelectionEmp = ^(NSArray *list){
            NSMutableArray *userList = [[NSMutableArray alloc] init];
            for (ICEmpInfoModel *user in list) {
                NSDictionary *dict = @{@"userId":user.eId,
                                       @"userName":user.eName,
                                       @"photoId":user.photoId == nil ? @"":user.photoId};
                [userList addObject:dict];
            }
            
            NSDictionary *args = @{@"success":@true,
                                   @"data":userList};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.selectUser"] args:args] toWebView:self.webView];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    else if ([functionName isEqualToString:@"system.selectOrg"]) { // 选择组织
        ICOrganizationViewController *vc = [[ICOrganizationViewController alloc] init];
        vc.singleSelect = NO;
        vc.CtrStyle = CtrStyleSelectNode_sdk;
        vc.kkGroupType = GroupStyleSelectNode;
        vc.singleSelectionOrg = ^(NSArray *list){
            ICEmpInfoModel *org = [list firstObject];
            NSDictionary *dict = @{@"orgId":org.oId,
                                   @"orgName":org.oName};
            NSDictionary *args = @{@"success":@true,
                                   @"data":dict};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.selectOrg"] args:args] toWebView:self.webView];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    else if ([functionName isEqualToString:@"system.openChat"]) { // 打开双人会话
        if (![ICTools isObjEmpty:param]) {
            [self openTwoGroupParam:param];
            NSDictionary *args = @{@"success":@true};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.openChat"] args:args] toWebView:self.webView];
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"创建会话失败"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.openChat"] args:args] toWebView:self.webView];
        }
    }
    
    else if ([functionName isEqualToString:@"system.selectGroup"]) { // 选择会话
        ICConversationListViewController *vc = [[ICConversationListViewController alloc] init];
        vc.SelectGroupBlock = ^(ICGroup *group){
            NSDictionary *args = @{@"success":@true,
                                   @"error":@"",
                                   @"data":@{@"groupId":group.gId,
                                             @"groupName":group.gName,
                                             @"photoId":group.photoId == nil ? @"":group.photoId,
                                             @"type":group.gType}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.selectGroup"] args:args] toWebView:self.webView];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    else if ([functionName isEqualToString:@"system.openGroupChat"]) { // 打开会话
        if (![ICTools isObjEmpty:param] && ![ICTools stringEmpty:param[@"groupId"]]) {
            [self openGroupChatGroupId:param[@"groupId"]];
            NSDictionary *args = @{@"success":@true};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.openGroupChat"] args:args] toWebView:self.webView];
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"打开会话失败"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.openGroupChat"] args:args] toWebView:self.webView];
        }
    }
    
    else if ([functionName isEqualToString:@"system.showUserInfo"]) { // 打开人员详情页面
        if (![ICTools isObjEmpty:param] && ![ICTools stringEmpty:param[@"userId"]]) {
            NSDictionary *eId = @{@"eId":param[@"userId"]};
            if ([param[@"userId"] rangeOfString:@"bot"].location == NSNotFound) {
                if ([[iComeApiManager instance].delegate respondsToSelector:@selector(openContactsDetailsFromViewC::)]) {
                    [[iComeApiManager instance].delegate openContactsDetailsFromViewC:self:eId];
                } else {
                    UIViewController * vc = [[IComMediator sharedInstance] IComMediator_ContactsDetailViewController:eId];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }else
            {
                UIViewController *vc = [[IComMediator sharedInstance] IComMediator_RobotDetailViewController:eId];
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"用户Id为空"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.showUserInfo"] args:args] toWebView:self.webView];
        }
    }
    
    else if ([functionName isEqualToString:@"device.call"]) { // 打电话
        if (![ICTools isObjEmpty:param] && ![ICTools stringEmpty:param[@"tel"]]) {
            UIWebView * callWebview =[[UIWebView alloc] init];
            NSURL *telURL =[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", param[@"tel"]]];
            [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
            [[UIApplication sharedApplication].keyWindow addSubview:callWebview];
        }
    }
    
    else if ([functionName isEqualToString:@"device.scanQRCode"]) { // 扫一扫
        UIViewController *viewController = [[IComMediator sharedInstance] IComMediator_RCodeViewController:nil handler:^(id result) {
            NSDictionary *args = @{@"success":@true,@"data":@{@"qrcode":result[@"url"]}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"device.scanQRCode"] args:args] toWebView:self.webView];
        }];
        [self.navigationController pushViewController:viewController animated:YES];
        
        //        ICRCodeViewController *vc = [[ICRCodeViewController alloc] init];
        //        [vc setScanCompleteBlock:^(NSString *url) {
        //            NSDictionary *args = @{@"success":@true,@"data":@{@"qrcode":url}};
        //            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"device.scanQRCode"] args:args] toWebView:self.webView];
        //        }];
        //        [self.navigationController pushViewController:vc animated:YES];
    }
    
    else if ([functionName isEqualToString:@"system.shareToICOM"]) {
        if (![ICTools isObjEmpty:param]) {
            [self shareInfoToSession:param];
            
            //            NSDictionary *args = @{@"success":@true};
            //            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.shareToICOM"] args:args] toWebView:self.webView];
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"打开分享失败，参数不正确"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.shareToICOM"] args:args] toWebView:self.webView];
        }
    }
    
    else if ([functionName isEqualToString:@"system.shareToWechat"]) {
        
    }
    
    else if ([functionName isEqualToString:@"webview.setOptionMenu"]) {
        if (![ICTools isObjEmpty:param]) {
            if ([ICTools stringEmpty:param[@"iconId"]]) {
                NSDictionary *args = @{@"success":@false,
                                       @"data":@{@"errno":@"1",@"error":@"iconId不为空"}};
                [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.setOptionMenu"] args:args] toWebView:self.webView];
            } else if ([ICTools stringEmpty:param[@"onClick"]]) {
                NSDictionary *args = @{@"success":@false,
                                       @"data":@{@"errno":@"1",@"error":@"onClick不为空"}};
                [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.setOptionMenu"] args:args] toWebView:self.webView];
            } else {
                NSDictionary *args = @{@"success":@true};
                [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.setOptionMenu"] args:args] toWebView:self.webView];
                self.jsOptionMenuParam = param;
                [self setNavOptionMenu:param];
            }
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"参数错误"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.setOptionMenu"] args:args] toWebView:self.webView];
        }
    }
    
    else if ([functionName isEqualToString:@"webview.showOptionMenu"]) {
        [self setNavOptionMenu:self.jsOptionMenuParam];
    }
    
    else if ([functionName isEqualToString:@"webview.hideOptionMenu"]) {
        [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamedFromiComeSDKBundle:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(hideOptionMenuAction)]];
        self.navigationItem.rightBarButtonItem = nil;
        
        NSDictionary *args = @{@"success":@true,
                               @"data":@""};
        [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"webview.hideOptionMenu"] args:args] toWebView:self.webView];
    }
    
    else if ([functionName isEqualToString:@"webview.showCloseBtn"]) {
        [self setRightBarButtonItemsWithCloseItem:YES];
    }
    
    else if ([functionName isEqualToString:@"webview.hideCloseBtn"]) {
        [self setRightBarButtonItemsWithCloseItem:NO];
    }
    
    else if ([functionName isEqualToString:@"device.previewFile"]) { // 下载 、预览 文件
        NSString *url = param[@"serverUrl"];
        
        NSArray *types = [[url pathExtension] componentsSeparatedByString:@"?"];
        NSString *fileType = [types firstObject];
        
        // 文件名
        NSString *name = [[param[@"ext"] objectForKey:@"fileName"] stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@", [types firstObject]] withString:@""];
        NSString *fileName = [name UTF8String]== nil ? [[url lastPathComponent] stringByDeletingPathExtension] : name;
        
        if ([ICTools stringEmpty:url] || [ICTools stringEmpty:fileName] || [ICTools stringEmpty:fileType]) {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"文件路径参数错误",@"serverUrl":url}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"device.previewFile"] args:args] toWebView:self.webView];
            return;
        }
        
        [ICNetworkHelper downloadWithURL:url fileDir:kChildPath fileName:[NSString stringWithFormat:@"%@.%@", fileName,fileType] progress:^(NSProgress *progress) {
        } success:^(NSString *filePath) {
            NSDictionary *args = @{@"success":@true,
                                   @"data":@""};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"device.previewFile"] args:args] toWebView:self.webView];
            
            ICFileScanController *scanVC = [[ICFileScanController alloc] init];
            scanVC.filePath = [[filePath stringByReplacingOccurrencesOfString:@"file://" withString:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            scanVC.orgName = [param[@"ext"] objectForKey:@"fileName"];
            [self.navigationController pushViewController:scanVC animated:YES];
            
        } failure:^(NSError *error) {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":error.domain,@"serverUrl":url}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"device.previewFile"] args:args] toWebView:self.webView];
        }];
    }
    
    else if ([functionName isEqualToString:@"device.uploadFile"]) { // 上传文件
        ICDocumentViewController *docVC = [[ICDocumentViewController alloc] init];
        docVC.delegate = self;
        // ICNavigationController *nav = [[ICNavigationController alloc] initWithRootViewController:docVC];
        [self.navigationController pushViewController:docVC animated:YES];
    }
    else if([functionName isEqualToString:@"device.uploadFileToServer"])//上传文件到服务器
    {
        [self.actionSheet showInView:[ICTools lastWindow].rootViewController.view];
    }
    
    else if ([functionName isEqualToString:@"webview.setTitle"]) {
        [self setNavigationTitleView:param];
    }
    
    else if ([functionName isEqualToString:@"device.snapshot"]) {
        
        WEAKSELF
        [ICSystemTool checkCameraAuthorizationStatus:^{
            ICFaceDetectorViewController *vc = [[ICFaceDetectorViewController alloc] init];
            [vc setFaceDetector:^(NSData *faceImageData) {
                NSLog(@"人脸识别成功 回调给js");
                @autoreleasepool{
                    NSString *imageDataBase64 = [NSString base64EncodedStringWithData:faceImageData];
                    NSString *imageDataStr = [NSString stringWithFormat:@"data:%@;base64,%@", [ICJSBridgeTools typeForImageData:faceImageData],imageDataBase64];
                    
                    NSDictionary *args = @{@"success":@true,@"data":imageDataStr};
                    [weakSelf.jsBridge postNotificationJavaScript:[weakSelf returnResult:weakSelf.callbackMap[@"device.snapshot"] args:args] toWebView:weakSelf.webView];
                }
            }];
            [weakSelf presentViewController:vc animated:YES completion:nil];
        }];
    }
    
    else if ([functionName isEqualToString:@"system.openApp"]) {
        if (![ICTools isObjEmpty:param]) {
            [self pushCustomViewControllerURL:[param objectForKey:@"url"]
                           fromViewController:self
                                     animated:YES];
        }
    }
    else if ([functionName isEqualToString:@"system.loadUrlWithRefer"]) {
        if (![ICTools isObjEmpty:param]) {
            [[NSUserDefaults standardUserDefaults] setObject:param forKey:@"loadUrlWithRefer"];
            NSDictionary *args = @{@"success":@true};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.loadUrlWithRefer"] args:args] toWebView:self.webView];
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"参数不为空"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.loadUrlWithRefer"] args:args] toWebView:self.webView];
        }
    }
    
    else if ([functionName isEqualToString:@"system.createMultChat"]) {
        //        param = @{@"eId":@"10050828",@"eIds":@[@"10050342",@"10041135",@"10055585"],@"eName":@"linchuanbin",@"gName":@"qun name"};
        if (![ICTools isObjEmpty:param]) {
            NSLog(@"eIds = %@",param[@"eIds"]);
            NSMutableArray * mutableArray = param[@"eIds"];
            NSString *tempString = [mutableArray componentsJoinedByString:@","];
            if([tempString rangeOfString:[ICUser currentUser].eId].location !=NSNotFound)
            {
                NSLog(@"包含");
                
                if([mutableArray count] > 1){
                    [mutableArray removeObject:[ICUser currentUser].eId];
                    NSMutableDictionary * newdic = [[NSMutableDictionary alloc]init];
                    [newdic setDictionary:param];
                    [newdic setValue:mutableArray forKey:@"eIds"];
                    [self createMultChatParam:newdic];
                }else{
                    NSDictionary *args = @{@"success":@false,
                                           @"data":@{@"no":@2,
                                                     @"msg":@"请至少添加一个用户"}};
                    [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.createMultChat"] args:args] toWebView:self.webView];
                }
            }
            else
            {
                NSLog(@"不包含");
                [self createMultChatParam:param];
            }
        } else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"创建多人会话失败"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.createMultChat"] args:args] toWebView:self.webView];
        }
    }
    else if ([functionName isEqualToString:@"system.openVideoConf"]) {
        if (![ICTools isObjEmpty:param]) {
            
            NSLog(@"param = %@",param);
            NSDictionary *args = @{@"success":@true,@"data":@"打开视频会议详情成功"};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.openVideoConf"] args:args] toWebView:self.webView];
        }else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"打开视频会议失败"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.openVideoConf"] args:args] toWebView:self.webView];
        }
    }
    
    //    c.system.openNativeMethod({
    //        "method":"showNews",
    //        "param":{"title": "?",
    //            "news" : [{"dId":"?","dName":""},{"dId":"?","dName":"?"},...]}
    //    },
    //                              success : function(data) {},
    //                              fail : function(err) {}
    //                              });
    else if ([functionName isEqualToString:@"system.openNativeMethod"]) {
        if (![ICTools isObjEmpty:param]) {
            if ([[iComeApiManager instance].delegate respondsToSelector:@selector(openWebViewNativeMethodsWithParams:::)]) {
                [[iComeApiManager instance].delegate openWebViewNativeMethodsWithParams:param :self :^(id result) {
                    NSDictionary *args = @{@"success":@true,@"data":result};
                    [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.openCustomMethod"] args:args] toWebView:self.webView];
                }];
            }
        }else {
            NSDictionary *args = @{@"success":@false,
                                   @"data":@{@"errno":@"1",@"error":@"失败"}};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.openCustomMethod"] args:args] toWebView:self.webView];
        }
    }
}


- (void)setNavigationTitleView:(NSDictionary *)param {
    if (![ICTools isObjEmpty:param]) {
        self.navigationItem.titleView = self.navTitleView;
        self.navTitleView.titleLabel.text = param[@"title"];
        self.navTitleView.status = YES;
        if (![ICTools stringEmpty:param[@"onClick"]]) {
            self.navTitleView.statusView.hidden = NO;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
                self.navTitleView.status = !self.navTitleView.status;
                
                [self.jsBridge postNotificationJavaScript:[NSString stringWithFormat:@"%@()", param[@"onClick"]] toWebView:self.webView];
            }];
            [self.navTitleView addGestureRecognizer:tap];
        } else {
            self.navTitleView.statusView.hidden = YES;
        }
    }
}

- (void)hideOptionMenuAction {
    NSLog(@"hideOptionMenuAction");
}

- (void)selectedFileName:(NSString *)fileName {
    NSLog(@"self.callName =%@",self.callName);
    __block NSString *callBackName = self.callName;
    //@"device.uploadFile";
    NSString *filePath = [[ICFileTool fileMainPath] stringByAppendingPathComponent:fileName];
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];
    NSString *trueName = [fileName originName];
    NSString *serverUrl = self.jsRequestParam[callBackName][@"serverUrl"];
    NSString *mimeType = [ICFileTool getMIMETypeWithCAPIAtFilePath:filePath];
    NSDictionary *parameters = @{@"fileName" : trueName};
    
    [ICNetworkHelper uploadWithURL:serverUrl parameters:parameters data:fileData name:nil fileName:trueName mimeType:mimeType progress:^(NSProgress *progress) {
        NSLog(@"Progress is %f", progress.fractionCompleted);
    } success:^(id responseObject) {
        NSString *fileId = [responseObject objectForKey:@"fileId"];
        NSLog(@"上传成功");
        if([callBackName isEqualToString:@"device.uploadFile"]){
            
            if (![ICTools stringEmpty:fileId]) {
                NSDictionary *args=@{@"success":@true,@"data":@{@"fileId":fileId}};
                [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[callBackName] args:args] toWebView:self.webView];
            } else {
                NSDictionary *args = @{@"success":@false,
                                       @"data":@{@"errno":@"1",
                                                 @"error":[responseObject objectForKey:@"error"] == nil ? @"fileId为空":[responseObject objectForKey:@"error"],
                                                 }};
                [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[callBackName] args:args] toWebView:self.webView];
            }
        }else{
            
            NSDictionary *args=@{@"success":@true,@"data":responseObject};
            [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[callBackName] args:args] toWebView:self.webView];
        }
    } failure:^(NSError *error) {
        NSDictionary *args = @{@"success":@false,
                               @"data":@{@"errno":@(error.code),
                                         @"error":error.domain}};
        [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[callBackName] args:args] toWebView:self.webView];
    }];
}

- (void)cancelSelectedFile {
    NSString *callBackName = @"device.uploadFile";
    NSDictionary *args = @{@"success":@false,
                           @"data":@{@"errno":@"1",
                                     @"error":@"已取消"}};
    [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[callBackName] args:args] toWebView:self.webView];
}

- (NSString *)returnResult:(NSString *)callbackId args:(NSDictionary *)resultDic {
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resultDic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *resultArrayString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    [resultArrayString stringByReplacingOccurrencesOfString:@"\"" withString:@"'"];
    return [NSString stringWithFormat:@"iCom.handleMessageFromiCom(%@,%@);", callbackId, resultArrayString];
}

- (void)hideNavigationBar {
    self.statusBarView.height =  [ICTools isIPhoneX] ?44:20;
    self.isHideNavigationBar = YES;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)showNavigationBar {
    self.statusBarView.height = 0;
    self.isHideNavigationBar = NO;
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)setNavOptionMenu:(NSDictionary *)info {
    if ([ICTools isObjEmpty:info]) return;
    NSInteger iconId = [info[@"iconId"] integerValue];
    NSString *imageName = @"icon_js_plus";
    if (iconId == 1) {
        imageName = @"icon_js_plus";
    } else if (iconId == 2) {
        imageName = @"App_menubox_blue";
    } else if (iconId == 3) {
        imageName = @"icon_js_magnifier";
    } else if (iconId == 4) {
        imageName = @"App_menubox_blue";
    }
    self.navigationItem.rightBarButtonItem = nil;
    
    
    //    UIButton *informationCardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    if (iconId == 4) {
    //        [informationCardBtn addTarget:self action:@selector(rightBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    //    } else {
    //        [informationCardBtn addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //    }
    //    [informationCardBtn setImage:[UIImage imageNamedFromiComeSDKBundle:imageName] forState:UIControlStateNormal];
    //
    //    [informationCardBtn sizeToFit];
    //    UIBarButtonItem *informationCardItem = [[UIBarButtonItem alloc] initWithCustomView:informationCardBtn];
    //
    //
    //        UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //        fixedSpaceBarButtonItem.width = 15;
    //
    //
    //    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [settingBtn addTarget:self action:@selector(JSCloseWebView) forControlEvents:UIControlEventTouchUpInside];
    //    [settingBtn setImage:[UIImage imageNamedFromiComeSDKBundle:@"App_close_white"] forState:UIControlStateNormal];
    //    [settingBtn sizeToFit];
    //    UIBarButtonItem *settingBtnItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
    //
    //    self.navigationItem.rightBarButtonItems  = @[informationCardItem,fixedSpaceBarButtonItem,settingBtnItem];
    
    
    UIButton *informationCardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (iconId == 4) {
        [informationCardBtn addTarget:self action:@selector(rightBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    } else {
        [informationCardBtn addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    [informationCardBtn setImage:[UIImage imageNamedFromiComeSDKBundle:imageName] forState:UIControlStateNormal];
    informationCardBtn.bounds = CGRectMake(0, 0, 33, 33);
    [informationCardBtn setImageEdgeInsets:UIEdgeInsetsMake(7, 7, 7, 7)];
    UIBarButtonItem *informationCardItem = [[UIBarButtonItem alloc] initWithCustomView:informationCardBtn];
    
    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    settingBtn.bounds = CGRectMake(0, 0, 33, 33);
    [settingBtn setImageEdgeInsets:UIEdgeInsetsMake(7, 14, 7, 0)];
    [settingBtn addTarget:self action:@selector(JSCloseWebView) forControlEvents:UIControlEventTouchUpInside];
    [settingBtn setImage:[UIImage imageNamedFromiComeSDKBundle:@"App_close_white"] forState:UIControlStateNormal];
    UIBarButtonItem *settingBtnItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
    self.navigationItem.rightBarButtonItems  = @[informationCardItem,settingBtnItem];
}

- (void)rightButtonAction:(UIButton *)sender {
    [self.jsBridge postNotificationJavaScript:[NSString stringWithFormat:@"%@()", self.jsOptionMenuParam[@"onClick"]] toWebView:self.webView];
}

-(void)JSCloseWebView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)openTwoGroupParam:(NSDictionary *)object {
    
    
    NSDictionary *param = @{@"eId":[ICUser currentUser].eId,
                            @"eName":object[@"userName"],
                            @"otherId":object[@"userId"]};
    WEAKSELF
    [ICCreateGroupManager createTwoGroupWith:param Handle:^(ICGroup *group) {
        if (group) {
            ICChatViewController *vc = [[ICChatViewController alloc] init];
            vc.group = group;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
        
    }];
}

-(void)createMultChatParam:(NSDictionary *)object
{
    WEAKSELF
    [ICCreateGroupManager createMultGroupWith:object Handle:^(ICGroup *group) {
        ICChatViewController *vc = [[ICChatViewController alloc] init];
        vc.group = group;
        NSDictionary *args = @{@"success":@true,
                               @"data":@{@"gId":group.gId,@"photoId":group.photoId == nil ? @"":group.photoId}};
        [self.jsBridge postNotificationJavaScript:[self returnResult:self.callbackMap[@"system.createMultChat"] args:args] toWebView:self.webView];
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    }];
}

- (void)openGroupChatGroupId:(NSString *)groupId {
    ICGroup *group = [[ICGroup alloc] init];
    group.gId = groupId;
    ICChatViewController *vc = [[ICChatViewController alloc] init];
    vc.group = group;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
